/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InvitarAmigos.java
 *
 * Created on 10-nov-2011, 20:22:58
 */
package Ventanas;

import Objetos.PropiedadesIP;
import Proxies.CtrlUsuariosWS;
import Proxies.CtrlUsuariosWSService;
import Proxies.DataUsuario;
import Proxies.StringArray;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.xml.namespace.QName;

/**
 *
 * @author Usuario
 */
public class InvitarAmigos extends javax.swing.JDialog {

    StringArray amigos;
    String invitado;
    Conversacion conversa;

    /** Creates new form InvitarAmigos */
    public InvitarAmigos(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

    }

    public InvitarAmigos(java.awt.Frame parent, boolean modal, Conversacion conv, StringArray amigosEnComun) {
        super(parent, modal);
        initComponents();
        amigos = amigosEnComun;
        conversa = conv;
        cargarAmigos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtable_amigos = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        Inivitar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jtable_amigos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nick", "Correo", "Nombre"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtable_amigos);

        jLabel1.setText("Amigos En Comun");

        Inivitar.setText("Invitar");
        Inivitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InivitarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(128, 128, 128)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(23, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(271, Short.MAX_VALUE)
                .addComponent(Inivitar)
                .addGap(80, 80, 80))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                .addComponent(Inivitar)
                .addGap(46, 46, 46))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void InivitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InivitarActionPerformed

        // TODO add your handling code here:
        invitado = jtable_amigos.getValueAt(jtable_amigos.getSelectedRow(), 1).toString();
        conversa.agregarInvitado(invitado);
        this.setVisible(false);

    }//GEN-LAST:event_InivitarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                InvitarAmigos dialog = new InvitarAmigos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Inivitar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtable_amigos;
    // End of variables declaration//GEN-END:variables

    private void cargarAmigos() {

        PropiedadesIP props = PropiedadesIP.getInstance();
        String IP = props.getServidorCentralIP();
        URL dir2 = null;
        try {
            dir2 = new URL(IP + "CtrlUsuarios?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(SeleccAmigos.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNa2 = new QName("http://WebServices/", "CtrlUsuariosWSService");
        CtrlUsuariosWS ctrlUsr = new CtrlUsuariosWSService(dir2, qNa2).getCtrlUsuariosWSPort();

        DefaultTableModel model = (DefaultTableModel) jtable_amigos.getModel();
        Iterator<String> it = amigos.getItem().iterator();
        DataUsuario du;
        model.setRowCount(0);
        Object[] fila = new Object[3];
        while (it.hasNext()) {

            du = ctrlUsr.getUsuario(it.next());
            fila[0] = du.getNick();
            fila[1] = du.getCorreo();
            fila[2] = du.getNombre();
            model.addRow(fila);

        }


    }

    public String agregarAmigo() {
        return invitado;
    }
}
